<template>
  <!--
      作者：luoyiming
      时间：2020-06-01
      描述：插件中心-文章-添加
    -->
  <div class="product-add pb50">

    <!--添加文章-->
    <el-form size="small" :model="form" ref="form" :rules="rules" label-width="100px">
      <el-tabs v-model="activeName">
        <el-tab-pane label="常规选项" name="0">
          <template>
            <div class="common-form">添加内容</div>
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" class="max-w460"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="desc">
              <el-input type="textarea" rows="3":maxlength="100" show-word-limit  v-model="form.desc" placeholder="请输入描述" class="max-w460"></el-input>
            </el-form-item>
            <el-form-item label="所属栏目">
              <el-select size="small" v-model="form.menu_id" placeholder="所有栏目">
                <el-option v-for="(item, index) in menu" :key="index" :label="item.name" :value="item.menu_id"
                  :disabled="item.disabled">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="缩略图">
              <div>
                <el-button type="primary" @click="openUpload">上传图片</el-button>
                <img :src="path" class="mt10" width="120px;" v-if="isImg" :isImg="isImg" />
                <!--上传图片组件-->
                <Upload v-if="isupload" :isupload="isupload" @returnImgs="returnImgsFunc">上传图片</Upload>
              </div>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">显示</el-radio>
                <el-radio :label="0">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="内容详情">
              <div class="edit_container">
                <Uediter v-model="form.content" :text="ueditor.text" :config="ueditor.config" ref="ue"></Uediter>
              </div>
            </el-form-item>
            <el-form-item label="排序">
              <el-input type="number" v-model="form.sort" placeholder="请输入数字" class="max-w460"></el-input>
            </el-form-item>
          </template>
        </el-tab-pane>
        <el-tab-pane label="SEO选项" name="1">
          <el-form-item label="SEO标题" prop="seo_title">
            <el-input v-model="form.seo_title" placeholder="请输入SEO标题" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="SEO关键词" prop="seo_keywords">
            <el-input v-model="form.seo_keywords" type="textarea" :rows="2" placeholder="请输入SEO关键词" class="max-w460">
            </el-input>
          </el-form-item>
          <el-form-item label="SEO描述" prop="seo_description">
            <el-input v-model="form.seo_description" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入SEO描述" class="max-w460"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="其他选项" name="2">
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" placeholder="请输入作者" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="来源" prop="origin">
            <el-input v-model="form.origin" placeholder="请输入来源" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="浏览量" prop="click">
            <el-input v-model="form.click" placeholder="请输入浏览量" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="发布时间" prop="release_time">
            <el-date-picker v-model="form.release_time" type="datetime" placeholder="请选择发布时间"></el-date-picker>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>

      <!--提交-->
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>

    </el-form>
  </div>
</template>

<script>
  import InfoApi from '@/api/info.js';
  import Uediter from '@/components/UE.vue';
  import Upload from '@/components/file/Upload';
  export default {
    components: {
      /*编辑器*/
      Uediter,
      /*图片上传*/
      Upload: Upload
    },
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
        activeName: '0',
        /*是否上传图片*/
        isupload: false,
        isImg: false,
        /*富文本配置*/
        ueditor: {
          text: '',
          config: {
            initialFrameWidth: 800,
            initialFrameHeight: 500
          }
        },
        path: '',
        /*form表单数据*/
        form: {
          title: '',
          image: '',
          sort: 1,
          status: 1,
          content: '',
          desc: '',
          menu_id: 0,
          images: [],
          seo_title: '',
          seo_keywords: '',
          seo_description: '',
          author: '',
          origin: '',
          release_time: ''
        },
        /*栏目*/
        menu: [],
        loading: false,
        /*验证规则*/
        rules: {
          title: [{
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }],
        },
        menu_id: 0,
      };
    },
    created() {
      this.menu_id = this.$route.query.menu_id;
      this.form.menu_id = this.$route.query.menu_id * 1;
      /*获取列表*/
      this.getMenuList();
      this.form.release_time = new Date();
    },
    methods: {
      /*上传*/
      openUpload() {
        this.isupload = true;
      },

      /*获取图片*/
      returnImgsFunc(e) {
        let self = this;
        if (e != null) {
          this.form.image = e[0].file_path;
          this.path = e[0].file_path;
          this.isImg = true;
        }
        this.isupload = false;
      },

      /*获取栏目*/
      getMenuList() {
        let self = this;
        let Params = {};
        Params.menu_id = this.menu_id;
        InfoApi.toAddInfo(Params, true)
          .then(res => {
            self.menu = res.data.menu;
          })
          .catch(error => {

          });
      },

      /*添加文章*/
      onSubmit() {
        let self = this;
        let form = self.form;
        self.form.content = this.$refs.ue.getUEContent();
        self.$refs.form.validate(valid => {
          if (valid) {
            self.loading = true;
            InfoApi.addInfo(form, true)
              .then(data => {
                self.loading = false;
                self.$message({
                  message: data.msg,
                  type: 'success'
                });
                self.$router.push('/info/info/index');
              })
              .catch(error => {
                self.loading = false;
              });
          }
        });
      },

      /*取消添加，返回文章列表*/
      cancelFunc() {
        this.$router.push({
          path: '/info/info/index'
        });
      }
    }
  };
</script>

<style>
  .edit_container {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
  }

  .ql-editor {
    height: 400px;
  }
</style>
